package com.example.idachuappone.index.item;

import java.io.Serializable;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.entity.CommentImg;
import com.example.idachuappone.index.ui.MyGridView;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class TwoMenuCommentListActivity extends BaseActivity implements OnItemClickListener, OnClickListener {
	private Button btn_back;
	private BitmapUtils bu;
	private List<CommentImg> list;
	private MyGridView mgv_menu_list;
	private MyAdapter myAdapter;
	private String title;
	private TextView tv_title;

	private class MyAdapter extends BaseAdapter {
		private BitmapUtils bu;
		private Context context;
		private List<CommentImg> list;

		public MyAdapter(Context context, BitmapUtils bu) {
			this.context = context;
			this.bu = bu;
		}

		public void setList(List<CommentImg> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			return this.list == null ? 0 : this.list.size();
		}

		@Override
		public Object getItem(int position) {
			return this.list == null ? null : (CommentImg) this.list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LinearLayout.inflate(this.context, R.layout.simple_two_menu_greed_item, null);
				holder.tv_menu_name = (TextView) convertView.findViewById(R.id.tv_menu_name);
				holder.img_menu_greed = (ImageView) convertView.findViewById(R.id.img_menu_greed);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			this.bu.display(holder.img_menu_greed, this.list.get(position).getUrl_small());
			holder.tv_menu_name.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			return convertView;
		}
	}

	private class ViewHolder {
		ImageView img_menu_greed;
		TextView tv_menu_name;

		private ViewHolder() {
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_two_menu_comment_list);
		this.bu = BitmapUtilHelper.createBitmapUtil(this);
		this.list = (List) getIntent().getExtras().getSerializable("list");
		this.title = getIntent().getExtras().getString("title");
		initView();
		initAdapter();
	}

	private void initAdapter() {
		this.myAdapter.setList(this.list);
		this.myAdapter.notifyDataSetChanged();
	}

	private void initView() {
		this.tv_title = (TextView) findViewById(R.id.tv_title);
		this.tv_title.setText(this.title);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.mgv_menu_list = (MyGridView) findViewById(R.id.mgv_menu_list);
		this.mgv_menu_list.setOnItemClickListener(this);
		this.myAdapter = new MyAdapter(this, this.bu);
		this.mgv_menu_list.setAdapter(this.myAdapter);
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
		Intent intent = new Intent(this, CommentGreensListActivity.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("list", (Serializable) this.list);
		bundle.putInt("index", position);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		default:
		}
	}
}
